<template>
	<view>
		<view class="search-box">
		  <uni-search-bar @confirm="search" @input="input" :radius="100" :cancelButton="none"></uni-search-bar>
		</view>
    <!-- 搜索结果区域 -->
    <view class="sugg-list" v-if="searchList.length !== 0">
      <view class="sugg-item" v-for="(item, i) in searchList" :key="i">
        <view class="goods-name">{{ item.goods_name }}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 历史搜索的区域 -->
    <view class="history-box" v-else>
      <!-- 历史搜索的标题 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <!-- 历史搜索的数据列表 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in history" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 延时器
        timer: null,
        // 搜索关键词
        kw: '',
        // 搜索的结果列表
        searchList: [],
        // 历史搜索的数据
        historyList: []
			};
		},
    methods: {
      input (e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e.value
          this.getSearchList()
        },500)
      },
      // 获取搜索结果的数据
      async getSearchList () {
        if (this.kw.length === 0) {
          this.searchList = []
          return
        }
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchList = res.message
        this.saveSearchHistory()
      },
      // 保存搜索过的历史关键词
      saveSearchHistory () {
        // 处理已经出现过的关键词
        const set = new Set(this.historyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList = Array.from(set)
        // 将历史搜索记录持久化的存储到本地
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      // 清空历史记录
      clean () {
        this.historyList = []
        uni.setStorageSync('kw', '[]')
      },
      // 点击历史记录进入对应的数据列表
      gotoGoodsList (kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    },
    onLoad() {
      // 获取历史搜索记录
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    computed:{
      // 处理后搜索的关键词展示在历史搜索的数据前面
      history () {
        return [...this.historyList].reverse()
      }
    }
	}
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list{
  padding: 0 5px;
  .sugg-item{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 0;
    border-bottom: 1px solid #efefef;
    .goods-name{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
.history-box{
  padding: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    height: 30px;
    align-items: center;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    uni-tag{
      margin: 5px 5px 0 0;
    }
  }
}
</style>
